//
//  SearchViewController.m
//  A_Project_DLS151129_SQ
//
//  Created by 151129-49宋强 on 16/1/25.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchTableViewCell.h"
#define Width self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface SearchViewController ()<UISearchBarDelegate,UITableViewDataSource, UITableViewDelegate, UISearchControllerDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSString *inputStr;//搜索框输入内容
@property (nonatomic, retain)NSMutableArray *dataArr;
@end

@implementation SearchViewController
- (void)dealloc
{   [_dataArr release];
    [_tableView release];
    [_inputStr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatSearchBar];//创建searchBar
    [self creatTableView];//创建tableView
    self.inputStr = @"";
    //初始化数组
    self.dataArr = [NSMutableArray array];
    [self dataHandle];//数据处理
    
}
#pragma mark - 数据处理
- (void)dataHandle
{
    NSString *urlStr = @"http://api2.pianke.me/search/get";
  //  NSDictionary *dic = @{@"start": @0, @"client": @2, @"auth": @"Dc1oFnrm1pPhDCsJ9H8Tum1TJxQJKiPbeP8GxoDzY0cWP229ZIQxVf6m3k", @"limit": @10, @"type": @"user", @"keyword":self.inputStr};
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@0, @"start", @2, @"client", @"Dc1oFnrm1pPhDCsJ9H8Tum1TJxQJKiPbeP8GxoDzY0cWP229ZIQxVf6m3k", @"auth" , @10, @"limit", @"user", @"type", self.inputStr, @"keyword", nil];
    [PWNetWorking POSTWithURL:urlStr body:dic completion:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        self.dataArr = [Model modelHanderWithArray:arr];
        [self.tableView reloadData];
    }];
}
#pragma mark - 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Width, HEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    [_tableView release];
    //注册重用池
    [_tableView registerClass:[SearchTableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
#pragma mark - cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    Model *m = _dataArr[indexPath.row];
#pragma mark - 高亮设置
    NSMutableAttributedString *atemStr = [[NSMutableAttributedString alloc] initWithString:m.uname];
    NSRange range = [m.uname rangeOfString:_inputStr];
    NSLog(@"%@ %@",m.uname, _inputStr);
    [atemStr setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName: [UIFont systemFontOfSize:18]} range:range];
    cell.nameLabel.attributedText = atemStr;
    [cell.headImgView sd_setImageWithURL:[NSURL URLWithString:m.icon] placeholderImage:[UIImage imageNamed:@"coffe"]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
#pragma mark - 点击触发方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Model *m = _dataArr[indexPath.row];
    UserInfoViewController *userVC = [[UserInfoViewController alloc] init];
    userVC.uid = m.uid;
    [self.navigationController pushViewController:userVC animated:YES];
    [userVC release];
}
#pragma mark - 创建searchBar
- (void)creatSearchBar
{
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, Width, 80)];
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    searchBar.showsCancelButton = YES;
    searchBar.delegate = self;
    searchBar.placeholder = @"搜索";
    self.navigationItem.titleView = searchBar;
    [searchBar release];
}
//取消返回上一个页面
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = NO;
}
#pragma mark - 协议方法
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //获取输入的字符
    self.inputStr = searchText;
    [self dataHandle];
}
//点击搜索触发
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"%@", self.inputStr);
    [searchBar resignFirstResponder];//回收键盘
    [self dataHandle];//加载
    if (_dataArr.count == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有搜索结果"preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:action];
       [self presentViewController:alert animated:YES completion:^{
           
       }];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
